<template>
  <div class="page-modal">
    <el-dialog
      title="编辑用户"
      v-model="EditdialogVisible"
      width="40%"
      center
      destroy-on-close
    >
      <HyForm v-bind="modalConfig" v-model="formData"></HyForm>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="EditdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from "vue";
import { useStore } from "vuex";

import HyForm from "@/base-ui/form";

export default defineComponent({
  components: {
    HyForm
  },
  props: {
    modalConfig: {
      type: Object,
      required: true
    },
    defaultInfo: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props) {
    const store = useStore();
    const EditdialogVisible = ref(false);
    const formData = ref<any>({});

    watch(
      () => props.defaultInfo,
      (newValue) => {
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`];
        }
      }
    );

    // 点击确定按钮的逻辑
    const handleConfirmClick = () => {
      EditdialogVisible.value = false;
      if (Object.keys(props.defaultInfo).length) {
        // 编辑
        console.log("编辑用户");
        store.dispatch("system/editPageDataAction", {
          // pageName: props.pageName,
          ...formData.value
          // id: props.defaultInfo.id
        });
      } else {
        // 新建
        // console.log('新建用户')
        // store.dispatch('system/createPageDataAction', {
        //   pageName: props.pageName,
        //   newData: { ...formData.value }
        // })
      }
    };

    return {
      EditdialogVisible,
      formData,
      handleConfirmClick
    };
  }
});
</script>

<style scoped></style>
